package com.wr.vehicle.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wr.common.annotation.Log;
import com.wr.common.core.controller.BaseController;
import com.wr.common.core.domain.AjaxResult;
import com.wr.common.enums.BusinessType;
import com.wr.vehicle.domain.Ebike;
import com.wr.vehicle.service.IEbikeService;
import com.wr.common.utils.poi.ExcelUtil;
import com.wr.common.core.page.TableDataInfo;

import java.util.Map;

/**
 * 车辆信息Controller
 * 
 * @author art8173
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/vehicle/ebike")
public class EbikeController extends BaseController
{
    @Autowired
    private IEbikeService ebikeService;

    /**
     * 导出二维码
     *
     * @param params 前端上传的参数，键值对 licensePlate: qrId
     * @return AjaxResult
     */
    @PostMapping("/exportQRCode")
    public ResponseEntity<byte[]> exportQRCode(@RequestBody Map<String, String> params) {
        try {
            byte[] zipBytes = ebikeService.exportQrCodes(params);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "qr_codes.zip");

            return new ResponseEntity<>(zipBytes, headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }




    /**
     * 查询车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ebike ebike)
    {
        startPage();
        List<Ebike> list = ebikeService.selectEbikeList(ebike);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:export')")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ebike ebike)
    {
        List<Ebike> list = ebikeService.selectEbikeList(ebike);
        ExcelUtil<Ebike> util = new ExcelUtil<Ebike>(Ebike.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:query')")
    @GetMapping(value = "/{ebikeId}")
    public AjaxResult getInfo(@PathVariable("ebikeId") Long ebikeId)
    {
        return success(ebikeService.selectEbikeByEbikeId(ebikeId));
    }

    /**
     * 根据 qr_id 查询 license_plate
     *
     * @param qrId 二维码ID
     * @return AjaxResult 包含 license_plate
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:query')")
    @GetMapping("/licensePlateByQrId/{qrId}")
    public AjaxResult getLicensePlateByQrId(@PathVariable("qrId") String qrId) {
        String licensePlate = ebikeService.selectLicensePlateByQrId(qrId);
        if (licensePlate != null) {
            return success(licensePlate);
        } else {
            return error("未找到对应的 license_plate");
        }
    }

    /**
     * 获取车辆与违规详细信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:query')")
    @GetMapping(value = "/details/{ebikeId}")
    public AjaxResult getDetails(@PathVariable("ebikeId") Long ebikeId)
    {
        return success(ebikeService.selectDetailsByEbikeId(ebikeId));
    }

    /**
     * 新增车辆信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:add')")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ebike ebike)
    {
        return toAjax(ebikeService.insertEbike(ebike));
    }

    /**
     * 修改车辆信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:edit')")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ebike ebike)
    {
        return toAjax(ebikeService.updateEbike(ebike));
    }

    /**
     * 删除车辆信息
     */
    @PreAuthorize("@ss.hasPermi('vehicle:ebike:remove')")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ebikeIds}")
    public AjaxResult remove(@PathVariable Long[] ebikeIds)
    {
        return toAjax(ebikeService.deleteEbikeByEbikeIds(ebikeIds));
    }
}
